import hec.heclib.dss.HecDss as HecDss
import hec.heclib.dss.DSSPathname as DSSPathname
#from hec.heclib.dss import *
#from hec.hecmath import *
import os as os


def CleanDssCatalog(filePath):
    try:
        os.remove(filePath+".dsc")
    except:
        pass    
    

def CopyToFuture(inFilePath, outFilePath, beginYR, totalYR, futureYR = -99 ):

    if futureYR == -99:
        futureYR = beginYR
    
    endYR = totalYR + beginYR - 1
        
    timeWindow_begin = "02JAN"+str(beginYR)+" 0000"
    timeWindow_end =   "31DEC"+str(endYR)  +" 2400"
    
    shiftedYR = futureYR - beginYR
    
    inFile =  HecDss.open(inFilePath+".dss")
    inFile.setTimeWindow(timeWindow_begin, timeWindow_end)
    outFile = HecDss.open(outFilePath+".dss") 

  
    pathList = inFile.getCondensedCatalog()
    
    for i, p in enumerate(pathList):
        p = DSSPathname(str(p))
        p.setDPart("")
        pathList[i]=p.getPathname()
        
     
    for i, p in enumerate(pathList):
    
        print "p: "+ p
        outflow = inFile.read(p)
        futurePath = DSSPathname(outflow.getPath())
        futurePath.setFPart(str(beginYR)+"_"+str(endYR))
        outflow.setPathname(futurePath.getPathname())
        
        if shiftedYR == 0 :
            shiftedData = outflow
        else:
            shiftedData = outflow.shiftInTime(str(shiftedYR)+"Y")
        
        outFile.write(shiftedData)



def CopyWYTypesToFuture(beginYR, totalYR, futureYR ):
    

    tableFile_before=open('lookup/wytypes_before_writing.table','r') # read only
    tableFile_after=open('lookup/wytypes_after_writing.table','w') # write

    # copy whole content 
    data = tableFile_before.readlines()
    tableFile_after.writelines(data)

    dataWY = {} # this will map water year type to water year

    for line in data:
        line = line.replace('\n', '')
        element = line.strip().split()
        try:
            WY = int(element[0])

            if WY >= 1920:
                dataWY[WY] = line

        except:
            continue

    tableFile_after.writelines( '\n' )
    
    for i, year in enumerate(range(beginYR, beginYR + totalYR)):
         
        tableFile_after.writelines( str(futureYR+i) + dataWY[year][5:] + '\n')

        
def ArrayShift(inArray, shift): 

    outArray = map(lambda x: x+shift, inArray) 
    return outArray

def getUniquePath(inPath):
    outList = []
       
    for path in inPath:
        if path not in outList:
            outList.append(path)
    
    return outList

def getCleanPath(inPath):
    dict = {}
    
    
    for path in inPath:
        path = path.encode('ascii','ignore')
        path = path[1:-1]
        path = path.split('/')

        k = "/"+path[0]+"/"+path[1]+"/"+path[2]+"/01JAN1910/"+path[4]+"/"+path[5]+"/"
        
        if ( not dict.has_key(k)):
            content = {}
            content["A"]=path[0]
            content["B"]=path[1]
            content["C"]=path[2]
            content["D"]=path[4]
            content["E"]=path[5]
            dict[k] = content
    
    return dict

            
def gluePathname(a,b,c,e,f):
    
    s = "/"
    outString = s+a+s+b+s+c+"//"+e+s+f+s
    return outString

#def CopyData(inFile, arrayPathname, startTime, endTime):
    
    
